#ifndef CMYTCPSERVER_H
#define CMYTCPSERVER_H

#include <QSemaphore>
#include <QTcpServer>
#include <QThread>
#include <QTcpSocket>
#include <QObject>

#define CLIENTCOUNT 500

typedef struct {
    QString localIP;
    quint16 localPort;
} TCPSSetStruct;

typedef struct
{
    QString ip;
    quint16 port;
    int socketDescriptor;
    QTcpSocket tcpConn;
} tcpClient_t;

typedef struct
{
    QString ip;
    quint16 port;
    char data[128];
    unsigned int size;
} DataBufferStruct;

class CMyTCPServer : public QTcpServer
{
    Q_OBJECT
public:
    explicit CMyTCPServer(QObject *parent = 0);

    void SetParam(TCPSSetStruct set);
    bool OpenTcpS();
    void CloseTcpS();
    int  SendData(const char *data, int len, QString ip, quint16 port);
    bool IsConnected();
    TCPSSetStruct GetParam();
private:
    int FindSocket(QString ip, quint16 port);
protected:
    void incomingConnection(qintptr socketDescriptor);
private:
    TCPSSetStruct m_tcpSet;
    bool m_flag;
    tcpClient_t m_tcpClient[CLIENTCOUNT];

public slots:
    void processPendingDatagrams();//接受报文，处理
    void deleteConnection();
signals:
    void sigReceiveMsg(char *data, qint64 maxlen, QString host, quint16 port);
    void sigComingConnection(QString ip, quint16 port);
    void sigDeleteConnection(QString ip, quint16 port);
};

#endif // CMYTCPSERVER_H
